#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#define BUFFER_SIZE 100

int main(){
    pid_t pid;
    char buffer[BUFFER_SIZE];
    ssize_t numRead;
    switch (pid = vfork())
    {
    case -1:
        printf("Error %d %s\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    case 0:
        puts("Child process, enter value:");
        numRead = read(STDIN_FILENO, buffer, BUFFER_SIZE);
        if (numRead == -1){
            printf("Error read from STDIN_FILENO %d %s\n", errno, strerror(errno));
            _exit(EXIT_FAILURE);            
        }
        printf("Try to close STDIN_FILENO");
        if(close(STDIN_FILENO) == -1){
            printf("Error close STDIN_FILENO %d %s\n", errno, strerror(errno));
            _exit(EXIT_FAILURE);               
        }
        puts("Close STDIN_FILENO try to read:");
        numRead = read(STDIN_FILENO, buffer, BUFFER_SIZE);
        if (numRead == -1){
            printf("Error read from STDIN_FILENO %d %s\n", errno, strerror(errno));         
        }      
        _exit(EXIT_SUCCESS);  
    default:
        puts("Main process, try to read from STDIN_FILENO:");
        numRead = read(STDIN_FILENO, buffer, BUFFER_SIZE);
        if (numRead == -1){
            printf("Error read from STDIN_FILENO %d %s\n", errno, strerror(errno));
            exit(EXIT_FAILURE);            
        }
        buffer[numRead] = '\0';
        printf("Readed %ld value - %s\n",numRead ,  buffer);
        exit(EXIT_SUCCESS);
    }
}